package hotel.controller;

import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import hotel.entity.Category;
import hotel.entity.Room;
import hotel.service.ICategoryService;
import hotel.service.IRoomService;
import util.JSONUtil;

@Controller
public class RoomController {

	// Jackson
	private final static ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private IRoomService<Room> roomService;
	@Autowired
	private ICategoryService<Category> categoryService;
	
	@RequestMapping(value="roomManagement")
	public String roomManagement() {
		
		return "room/roomlist";
	}
	
	@ResponseBody
	@RequestMapping(value = "roomlist")
	public String getUserList(Model model) {
		String json = null;
		try {
			json = mapper.writeValueAsString(roomService.listAll("Room"));
			json = "{\"data\":" + json + "}";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping(value = "addRoom", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("categoryList", categoryService.listAll("Category"));
		return "room/add";
	}

	@ResponseBody
	@RequestMapping(value = "addRoom", method = RequestMethod.POST)
	public String addRoom(Room room) {
		if (roomService.save(room))
			return JSONUtil.returnSuccess();
		else
			return JSONUtil.returnFailure();
	}

	@RequestMapping(value = "editRoom", method = RequestMethod.GET)
	public String edit(int roomid, Model model) {
		Room room=roomService.findById(Room.class, roomid);
		model.addAttribute("room", room);
		model.addAttribute("categoryList", categoryService.listAll("Category"));
		model.addAttribute("checked", room.getCategory().getCategoryid());
		return "room/edit";
	}

	@ResponseBody
	@RequestMapping(value = "editRoom", method = RequestMethod.POST)
	public String editRoom(Room room) {
		if (roomService.update(room))
			return JSONUtil.returnSuccess();
		return JSONUtil.returnFailure();
	}

	@ResponseBody
	@RequestMapping(value = "deleteRoom")
	public String deleteRoom(int roomid) {
		if (roomService.deleteById("Room", roomid))
			return JSONUtil.returnSuccess();
		else
			return JSONUtil.returnFailure();
	}
	

	/**
	 * 获取房间Highchart数据
	 * @return
	 */
	@RequestMapping(value="getRoomStatistics")
	@ResponseBody
	public String getRoomStatistics() {
		return JSONUtil.roomStatisticsToJson(roomService.getRoomStatistics());
	}
	
	@RequestMapping(value="getAvailableRoomCount")
	@ResponseBody
	public String getAvailableRoomCount(int categoryid) {
		int available=roomService.getAvailableCountByCategory(categoryid);
		return "{\"statusCode\":\"200\",\"available\":"+available+"}";
	}
	
	@RequestMapping(value="readyRoom")
	@ResponseBody
	public String updateRoomState(int roomid) {
		if(roomService.updateRoomState(roomid))
			JSONUtil.returnSuccess();
		return JSONUtil.returnFailure();
	}
}
